package sfsoftware.game.actor.enemy;

import sfsoftware.util.Sound;
import sfsoftware.util.Tile;

public class SmartShooter extends SimpleShooter {

	public SmartShooter(int destX, int destY, String texture, int destWidth, int destHeight, Tile[][] assocTileMap) {
		super(destX, destY, texture, destWidth, destHeight, assocTileMap);
		setSrcY(36);
	}

	public SmartShooter(int destX, int destY, String texture, int srcX, int srcY, int destWidth, int destHeight, Tile[][] assocTileMap) {
		super(destX, destY, texture, srcX, srcY, destWidth, destHeight, assocTileMap);
		setSrcY(36);
	}	
		
	@Override
	public void update() {	
		// These jerks speed up if you are closer than 100 pixels away!
		if(distanceToPlayer() <= 100)
		{
			setSpeed(3);
		}
		else
		{
			setSpeed(1);
		}
		
		// Random chance they will fire!
		int shootOpportunity = ((int)(Math.random() * 200))  * shootFrequency;
		if(shootOpportunity == 0)
		{
			firingBullet = true;
		}
		
		// They only move when not in the middle of firing!
		if(!firingBullet)
		{
			switch(direction)
			{
				case 0:
					move(0, 1, tileMap);
					break;
				case 1:
					move(0, -1, tileMap);
					break;
				case 2:				
					move(1, 0, tileMap);
					break;
				case 3:
					move(-1, 0, tileMap);
					break;				
			}
		}

		if(firingCountdownClock.updateClock())
		{
			if(firingBullet)
			{
				firingCountdown++;
				if(firingCountdown > 4)
				{
					// Here is where we create a new SMART bullet!
					new SmartBullet((int)getX() + (width / 2) - 4, (int) getY() + (height / 2) - 4, "orb", 8, 8, tileMap, 15);
					Sound.playSound("shooter");
					firingCountdown = 0;
					firingBullet = false;
				}
			}
		}		
		
		if(animationTimer.updateClock())
		{
			if(getSrcX() == 0)
			{
				setSrcX(20);
			}
			else
			{
				setSrcX(0);
			}
		}
	}

}
